﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Linq;
using System.Web;
using System.Web.UI;
using vHCS.Web.MdlHR.Controls;
using vHCS.HR.Business;
using Telerik.Web.UI;
using System.Data;


/// 
/// [createDate:18/05/2011]
/// [author:NamLN]
/// <summary>
///  Lấy dữ liệu cho report
/// </summary>
///
namespace vHCS.Web
{
    public class Report
    {
        private MasterPage _MasterPage;
        private string _StoreName;
        private string[] _paraNames;   
        private object[] _paraValues; // Mang value tra ve TƯƠNG ỨNG với mảng  _paraNames 
        private int[] _paraTypes;     // Mang kiểu dữ liệu tra ve TƯƠNG ỨNG với mảng _paraValues 
                                      // 1:HR_ctrUserSearch; 2: DateTime; 3:String; 4:Int; 5:boolean 
        private string[] _paraNamesMulti;   
        //01/06/2011 Nam added
        private DataTable _tblPara;    // table cac tham so truyen vao store    
        private HR_ctrUserSearch_Multi _ctrUserSearch;
        private HR_ctrUserSearch_Report _ctrReportUserSearch;
        //01/06/2011 Nam added

        public Report(MasterPage page,string[] paraNames,string storeName)
        {
            this._MasterPage = page;
            this._StoreName = storeName;
            this._paraNames = paraNames;
            _paraValues = new object[_paraNames.Length];
            _paraTypes = new int[_paraNames.Length];
            getParameterOnPage();
            
        }
        public Report(MasterPage page, string[] paraNames)
        {
            this._MasterPage = page;            
            this._paraNames = paraNames;
            _paraValues = new object[_paraNames.Length];
            _paraTypes = new int[_paraNames.Length];
            getParameterOnPage();

        }
        public Report(MasterPage page,HR_ctrUserSearch_Report userSearch)
        {
            this._MasterPage = page;
            this._ctrReportUserSearch = userSearch;
            
        }
        //public Report(MasterPage page, HR_ctrEmpHeader_Report userSearch)
        //{
        //    this._MasterPage = page;
        //    this._ctrReportUserSearch = userSearch;

        //}
        //vannth
        public Report(MasterPage page, HR_ctrUserSearch_Multi userSearch)
        {
            this._MasterPage = page;
            this._ctrUserSearch = userSearch;
        }
        //vannth
       
        /// <summary>
        /// Lấy giá trị các control chứa trong Masterpage dựa vào tên của các control trong mảng _paraNames truyền
        /// </summary>
        public void getParameterOnPage()
        {
            
            object value = null;
            string prefix = "";
            string item = "";
            for (int i = 0; i < _paraNames.Length; i++)
            {
                item = _paraNames[i];                
                prefix = item.Substring(0, 3);                
                switch (prefix)
                {
                    case "ctr":
                        HR_ctrUserSearch_Report ctrSearch = ((HR_ctrUserSearch_Report)this._MasterPage.FindControl("MainContent").FindControl(item));
                        value = ctrSearch.ItemData;
                        _paraTypes[i] = 1;
                        break;
                    case "mul": //vannth
                        HR_ctrUserSearch_Multi multiSearch = ((HR_ctrUserSearch_Multi)this._MasterPage.FindControl("MainContent").FindControl(item));
                        value = multiSearch.ItemData;
                        _paraTypes[i] = 1;
                        break;   // vannth
                    case "dtp":
                        RadDatePicker dtpDate = ((RadDatePicker)this._MasterPage.FindControl("MainContent").FindControl(item));
                        if (dtpDate.SelectedDate == null)
                            value = null;
                        else
                            value = DateTime.Parse(dtpDate.SelectedDate.ToString());
                        _paraTypes[i] = 2;
                        break;
                    case "txt":
                        TextBox txtValue = ((TextBox)this._MasterPage.FindControl("MainContent").FindControl(item));
                        value = txtValue.Text;
                        _paraTypes[i] = 3;
                        break;
                    case "val":
                        RadNumericTextBox vNum = ((RadNumericTextBox)this._MasterPage.FindControl("MainContent").FindControl(item));
                        value = vNum.Value; // kieu double
                        break;                    
                    case "rdl":
                        RadioButtonList rdlList = ((RadioButtonList)this._MasterPage.FindControl("MainContent").FindControl(item));
                        if (rdlList.SelectedValue != "")
                            value = int.Parse(rdlList.SelectedValue.ToString());
                        else
                            value = null;
                        _paraTypes[i] = 4;
                        break;
                    case "chk":
                        CheckBox chkValue = ((CheckBox)this._MasterPage.FindControl("MainContent").FindControl(item));                        
                         value = chkValue.Checked;                        
                        _paraTypes[i] = 5;
                        break;
                    case "cbo":
                        RadComboBox cboValue = ((RadComboBox)this._MasterPage.FindControl("MainContent").FindControl(item));
                        if (cboValue.SelectedValue != "")
                            value = int.Parse(cboValue.SelectedValue.ToString());
                        else
                            value = null;
                        _paraTypes[i] = 4;
                        break;
                    default:
                        break;
                }
                // Reject prefix
                _paraNames[i] = item.Substring(3);
                _paraValues[i] = value;
            }
            
        }
        /// <summary>
        /// get data tu 1 store
        /// </summary>
        /// <returns>DataTable</returns>

        public DataTable getData()
        {
            HR_rptCollection rptReport = new HR_rptCollection();
            //vannth
             DataTable table = new DataTable();
            string prefix = "";
            prefix = _paraNames[0].Substring(0, 3);
            switch (prefix)
            {
                case "ctr":
                    table = rptReport.executeStore_multi(_paraNames, _paraValues, _paraTypes, _StoreName);
                    break;
                default:
                    table = rptReport.executeStore(_paraNames, _paraValues, _paraTypes, _StoreName);
                    break;
            }
           // DataTable table = rptReport.executeStore(_paraNames, _paraValues,_paraTypes, _StoreName);
            return table;
            //vannth
        }
        /// <summary>
        /// get data tu danh sach cac store
        /// <param name="lsStoreName":danh sach store></param>
        /// </summary>
        /// <returns>DataSet</returns>
        public DataSet getData(string[] lsStoreName)
        {
            HR_rptCollection rptReport = new HR_rptCollection();
            //vannth
            string prefix = "";
            DataSet ds = new DataSet();
            prefix = _paraNames[0].Substring(0, 3);
            switch (prefix)
            {
                case "ctr":
                    ds = rptReport.executeStore_multi(_paraNames, _paraValues, _paraTypes, lsStoreName);
                    break;
                default:
                    ds = rptReport.executeStore(_paraNames, _paraValues, _paraTypes, lsStoreName);
                    break;
                
            }
           // DataSet ds = rptReport.executeStore(_paraNames, _paraValues, _paraTypes,lsStoreName);            
            return ds;
            //vannth
        }

        /// <summary>
        ///  Lấy dữ liệu cho report dựa vào danh sách store truyền vào
        /// </summary>
        /// <param name="lsStoreName"></param>
        /// <returns>danh sách các table dữ liệu của report ứng với thứ tự trong mảng store truyền vào</returns>
        public DataSet getDataForReport(string[] lsStoreName)
        {            
            HR_rptCollection rptReport = new HR_rptCollection();
            _tblPara = rptReport.getParameterOnStore(lsStoreName[0]); // Lấy các parameter từ store
            for (int i = 0; i < _tblPara.Rows.Count; i++)
            {
                string columnName = _tblPara.Rows[i]["columnname"].ToString().Substring(1);// reject @
                _tblPara.Rows[i]["columnname"] = columnName;
                searchParaOnMasterPage(columnName, i);
            }
            //vannth
               //DataSet ds =new DataSet();
               // string prefix = "";
               // prefix = _paraNames[0].Substring(0, 3);
               // switch (prefix)
               // {
               //     case "ctr":
               //         ds = rptReport.executeStore_multi(_tblPara, lsStoreName, _ctrUserSearch == null ? null : _ctrUserSearch.ItemData);            
               //         break;
               //     default:
            DataSet  ds = rptReport.executeStore(_tblPara, lsStoreName, _ctrReportUserSearch == null ? null : _ctrReportUserSearch.ItemData);            
                //        break;
                //}
            
           //vannth
            return ds;

        }

        //public DataSet getDataForReport_1(string[] lsStoreName)
        //{
        //    HR_rptCollection rptReport = new HR_rptCollection();
        //    _tblPara = rptReport.getParameterOnStore(lsStoreName[0]); // Lấy các parameter từ store
        //    for (int i = 0; i < _tblPara.Rows.Count; i++)
        //    {
        //        string columnName = _tblPara.Rows[i]["columnname"].ToString().Substring(1);// reject @
        //        _tblPara.Rows[i]["columnname"] = columnName;
        //        searchParaOnMasterPage(columnName, i);
        //    }

        //    DataSet ds = rptReport.executeStore(_tblPara, lsStoreName, _ctrReportEmpHeader == null ? null : _ctrReportEmpHeader.ItemData);
            
        //    return ds;

        //}










        /// <summary>
        /// search giá trị của control dựa vào tên paraName (ứng với ID của control) 
        /// (Chỉ get giá trị cho các field không nằm trên _ctrUserSearch)
        /// </summary>
        /// <param name="paraName"></param>
        /// <param name="rowIndex"></param>
        /// <returns>control</returns>
        public Control searchParaOnMasterPage(string paraName,int rowIndex)
        {
            string fieldName = "";
            string typeName = "";
            object value = null;
            Control control= null;

            ControlCollection ctrMainContent = ((ControlCollection)this._MasterPage.FindControl("MainContent").Controls);
            foreach (Control item in ctrMainContent)
            {
                typeName = item.GetType().Name;
                switch (typeName)
                {
                    case "Label":                        
                        break;
                    case "RadDatePicker":  // DateTime
                        fieldName = item.ID.Substring(3); //reject 3 first character                        
                        if (fieldName == paraName)
                        {
                            control = ((RadDatePicker)this._MasterPage.FindControl("MainContent").FindControl(item.ID));
                            if (((RadDatePicker)control).SelectedDate == null)
                                value = null;
                            else
                                value = DateTime.Parse(((RadDatePicker)control).SelectedDate.ToString());                                                        
                        }
                        break;                    
                    case "TextBox":
                        fieldName = item.ID.Substring(3); //reject 3 first character                        
                        if(fieldName == paraName)
                        {
                            control  = ((TextBox)this._MasterPage.FindControl("MainContent").FindControl(item.ID));
                            value = ((TextBox)control).Text;                            
                        }
                        break;
                    case "HiddenField":
                        fieldName = item.ID.Substring(3); //reject 3 first character                        
                        if (fieldName == paraName)
                        {
                            control = ((HiddenField)this._MasterPage.FindControl("MainContent").FindControl(item.ID));
                            value = ((HiddenField)control).Value;
                        }
                        break;
                    case "RadNumericTextBox":
                        fieldName = item.ID.Substring(3); //reject 3 first character                        
                        if (fieldName == paraName)
                        {
                            control = ((RadNumericTextBox)this._MasterPage.FindControl("MainContent").FindControl(item.ID));
                            value = ((RadNumericTextBox)control).Value; // kieu double                            
                        }
                        break;
                    case "RadioButtonList":
                         fieldName = item.ID.Substring(3); //reject 3 first character                        
                         if (fieldName == paraName)
                         {
                             control = ((RadioButtonList)this._MasterPage.FindControl("MainContent").FindControl(item.ID));
                             if (((RadioButtonList)control).SelectedValue != "")
                                 value = int.Parse(((RadioButtonList)control).SelectedValue.ToString());
                             else
                                 value = null;                          
                         }
                         break;
                    case "CheckBox":
                         fieldName = item.ID.Substring(3); //reject 3 first character                        
                         if (fieldName == paraName)
                         {
                             control = ((CheckBox)this._MasterPage.FindControl("MainContent").FindControl(item.ID));
                             value = ((CheckBox)control).Checked;     
                         }

                        break;
                    case "RadComboBox":
                        fieldName = item.ID.Substring(3); //reject 3 first character                        
                        if (fieldName == paraName)
                        {
                             control = ((RadComboBox)this._MasterPage.FindControl("MainContent").FindControl(item.ID));
                             if (((RadComboBox)control).SelectedValue != "")
                                 value = ((RadComboBox)control).SelectedValue;
                             else
                                 value = null;
                        }
                        break;
                    default:
                        break;
                }

                if(control != null){
                    _tblPara.Rows[rowIndex]["value"] = value;
                    _tblPara.Rows[rowIndex]["isGet"] = true;
                    return control;
                }
                
            }

            return null;
            
        }
        
    }
}